clear all;
% close all;
clc
All_Mu=[11 10 11 11 11 10 10 11 13 8 11 11 12 10 11 10]
Subjects = {'A63','A64','A68','A69','A70','A72','A73','A74','A75','A76','A77','A78','A79','A80','A81','A82'};
Channels = 1:31;
hiddenlayers = 15;
Kfold = 10;
fs = 500;
dt = 1/fs;
f_Conditions_EMG_EEG_up
finger_names = {'Thumb';'Index';'Middle';'Ring';'Pinky'};
fingers = 'abcde';
pcolor = 'kbr';
Pre = 0; % on
dur = 3.5; % off
tsig_ori=Pre+dt:dt:dur;
NN = length(tsig_ori);
plot_triggers = 0;

for isub = 1:length(Subjects)
    Sub = Subjects{isub};
    Mu = All_Mu(isub);
    f_bands = [
        0.1 2.5 % SCP
        10 10 % Mu
        13 30 % betta
        30 50];% gamma
    for iband=1:size(f_bands,1)
        if iband==2 % Mu
            b(iband,:)=fir1(100,[Mu-1 Mu+1]./(fs/2));
        else
            b(iband,:)=fir1(100,f_bands(iband,:)./(fs/2));
        end

        for ifinger = 1:5
            ERP{iband,ifinger}= [];
        end
    end

    eval(['condall=' Sub '_all;']);
    for isession = 1:N_column:length(condall)
        chunkline = {condall{isession:isession+N_column-1}};
        if mod(isession,80)==1
            chunkline
        end
        finger = chunkline{1}(end);
        ifinger = find(fingers==finger);
        fname = ['EEG_data\' chunkline{2} ];
        eval(['load ' fname(1:end-4) '_P']);
        new_data2=f_montage(new_data);


        for iband=1:size(f_bands,1)

            for jc_channel = Channels
                xdata = conv(new_data2(jc_channel,:),b(iband,:),'same');
                new_data3(jc_channel,:)=xdata;
                p_all(jc_channel) = mean(new_data2(jc_channel,:).^2);
            end
            for i = 1:length(newTR_time) %  all triggers
                Ttime = newTR_time(i);
                [YY Ind] = min(abs(Ttime-tsig_long));
                for jc_channel = Channels
                    chunk = new_data3(jc_channel,Ind+round((1+Pre)*fs):Ind+round((1+Pre)*fs)+NN-1);
                    %                     power(jc_channel) = mean(chunk.^2);
                    power(jc_channel) = mean(chunk.^2)./ p_all(jc_channel);

                end
                ERP{iband,ifinger}=[ ERP{iband,ifinger}
                    power];
            end
            clear new_data3;
        end
        pause(0.1)
    end % isession

    for iband=1:size(f_bands,1)
        Xall = [];
        for i=1:5
            Xall=[
                Xall
                ERP{iband,i} ];
        end
        grand_avg = mean(Xall);
        for i=1:5
            All_vectors{i}=ERP{iband,i}-grand_avg;
        end
        Kernal = 1;
        %     Kernal = 5;
        out=f_runNN_Kfold(hiddenlayers,All_vectors,Kfold);
        %     out = f_lda_multi(All_vectors,Kernal,100,200)
        %                             out=f_lda_single(All_vectors);
        PC_all(isub,iband)=out.PC
        %     CM_all{isub}=out.CM;
    end
        clear ERP
        pause(0.1)
end % isub
figure
bar(PC_all.*100)
legend('SCP','Mu','Betta','Gamma')
ylim([0 100])
% exportgraphics(f1,['EEG_81.png'],'Resolution',300);
save pc_all_freq_EEG PC_all
